package cn.yajienet.data.elasticsearch.runner;

import cn.yajienet.data.elasticsearch.init.*;
import cn.yajienet.data.elasticsearch.properties.ElasticSearchProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Author Wang Chenguang
 * @Email wcg.chen@foxmail.com
 * @Date on 2021/12/18
 * @Version 1.0.0
 * @Description 初始化 spring boot 后，运行其他任务
 */
@Slf4j
@Component
public class ElasticsearchApplicationRunner implements ApplicationRunner {

    private final AutomaticInitialization automaticInitialization;
    private final SingleDocumentInitialization singleDocumentInitialization;
    private final AutomaticUpdate automaticUpdate;
    private final SingleDocumentUpdate singleDocumentUpdate;
    private final ElasticSearchProperties elasticSearchProperties;

    public ElasticsearchApplicationRunner(AutomaticInitialization automaticInitialization,
                                          SingleDocumentInitialization singleDocumentInitialization,
                                          AutomaticUpdate automaticUpdate,
                                          SingleDocumentUpdate singleDocumentUpdate,
                                          ElasticSearchProperties elasticSearchProperties) {
        this.automaticInitialization = automaticInitialization;
        this.singleDocumentInitialization = singleDocumentInitialization;
        this.automaticUpdate = automaticUpdate;
        this.singleDocumentUpdate = singleDocumentUpdate;
        this.elasticSearchProperties = elasticSearchProperties;
    }


    @Override
    public void run(ApplicationArguments args) {
        switch (elasticSearchProperties.getRunMode()) {
            case AUTO:
                automaticInitialization.init( args );
                automaticInitialization.listener( args );
                break;
            case AUTO_UPDATE:
                automaticUpdate.init( args );
                automaticUpdate.listener( args );
                break;
            case SINGLE:
                singleDocumentInitialization.init( args );
                singleDocumentInitialization.listener( args );
                break;
            case SINGLE_UPDATE:
                singleDocumentUpdate.init( args );
                singleDocumentUpdate.listener( args );
                break;
            default:
                log.warn( "not running" );
        }
        System.exit( 200 );
    }


}
